﻿@using admin.Model
@{
    ViewData["Title"] = "BookEdit";
    Layout = "~/Views/Shared/_SmallWinLayout.cshtml";

    var bookEdit = ViewData["bookEdit"] as AuthorOfBook;
    var date = bookEdit.book.PublicationDate;
    var d = date.ToString("yyyyMMdd");
}

<div class="page-container">
    <div class="form form-horizontal" id="form-article-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" placeholder="" value="@bookEdit.book.Name" name="bookname">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>作者信息：</label>
            <div class="formControls col-xs-8 col-sm-9">
                @for (int i = 0; i < bookEdit.authors.Count; i++)
                {
                    if (bookEdit.authors[i].Name != null)
                    {
                        <input type="text" class="input-text" value="作者@(i+1): @bookEdit.authors[i].Name " readonly unselectable="on">

                    }
                    else if (bookEdit.authors[i].Name == null)
                    {
                        <input type="text" class="input-text" value="尚未添加作者信息，请在编辑图书信息里添加" readonly unselectable="on">
                    }
                }
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>ISBN号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@bookEdit.book.Isbn" name="ISBN">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书分类：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@bookEdit.book.Type" placeholder="" id="" name="type">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>现库存量：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@bookEdit.book.Inventory" placeholder="" id="" name="inventory">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>总销售量：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@bookEdit.book.Countsale" placeholder="" id="" name="countsale">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>开本：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="format" id="" placeholder="" value="@bookEdit.book.Format" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>总页数：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="pagecount" id="" placeholder="" value="@bookEdit.book.PageCount" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>总字数：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="wordcount" id="" placeholder="" value="@bookEdit.book.WordCount" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书价格：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="price" id="" placeholder="" value="@bookEdit.book.Price" class="input-text auth" style="width:90%">
                元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>稿费：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="fee" id="" placeholder="" value="@bookEdit.book.Fee" class="input-text auth" style="width:90%">
                元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>责任编辑：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="edit" id="" placeholder="" value="@bookEdit.book.Editor" class="input-text auth" style="width:40%">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>出版日期：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="publicdate" value="@d" placeholder="" onfocus="WdatePicker({ dateFmt:'yyyyMMdd',maxDate:'#F{$dp.$D(\'datemax\')||\'%y%M%d\'}' })" id="datemin" class="input-text Wdate auth" style="width:60%">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书简介：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea name="content" type="text" cols="2" rows="5" class="textarea auth" placeholder="" id="bookcontent" datatype="*10-100" dragonfly="true" nullmsg="备注不能为空！">@bookEdit.book.Content</textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button onClick="book_edit('/Home/BookEditSave','@bookEdit.book.Id');" class="btn btn-secondary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
                <button onClick="book_author_info('修改图书作者信息','/Home/BookAuthorInfo','@bookEdit.book.Isbn');" class="btn btn-success radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 修改图书作者信息</button>
                <button onClick="book_author_info('图书作者信息','/Home/BookAuthorInfo','@bookEdit.book.Isbn');" class="btn btn-success radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 增加图书作者</button>
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //修改作者信息
    function book_author_info(title, url, isbn) {
        url = url + "?isbn=" + isbn;
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }




    //图书编辑提交的验证方法
    function book_edit(url, id) {
        url = url + '?id=' + id;
        //layer.msg("保存信息");
        var bookList = new Array();
        $('input[name = bookname]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '图书名不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = ISBN]').blur(function () {
            val = this.value;
            if (val.length < 10) {
                $(this).attr('placeholder', 'ISBN位数至少为10位');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = type]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '图书类型不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = inventory]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '库存量不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = countsale]').blur(function () {
            bookList.push($(this).val());
        });
        $('input[name = format]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '开本信息不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = pagecount]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '总页数不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = wordcount]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '总字数不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = price]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '价格不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = fee]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '稿费不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        $('input[name = edit]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '编辑不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        $('input[name = publicdate]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '出版日期不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('#bookcontent').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '内容简介不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        layer.confirm('确认要保存吗？', function () {
            bookList.length = 0;
            $('.auth').blur();
            tot = 0;
            $('.auth').each(function () {
                if ($(this).data('s') == 1) {
                    tot += 1;
                }
            });
            if (tot != 12) {
                //alert(tot);
                layer.msg('请完善图书信息再提交！');
            }
            else {
                //alert(tot);
                //已完成：添加，修改之前验证是否已存在相同的图书，ISBN唯一
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { 'additems': bookList.toString() },
                    success: function (id) {
                        if (id == "errorISBN") {
                            layer.msg('ISBN重复了，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                        }
                        else if (id == "errorName") {
                            layer.msg('图书名字已存在，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                        }
                        else if (id == "true") {
                            layer.msg('保存成功,图书信息已更新', { icon: 1, time: 5000 });
                            self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                window.parent.location.reload();   // 这个方法是刷新当前页面
                            }, 2000); //这里2000代表两秒
                        }
                        else {
                            layer.msg('保存失败，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                            //self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                            //    window.location.reload();   // 这个方法是刷新当前页面
                            //}, 2000) //这里2000代表两秒
                        }
                    },
                    error: function () {
                        layer.msg('保存失败了，请重新尝试编辑一下', { icon: 5, time: 4000 });
                    },
                });
            }

        });
    }
</script>